package ptkdemo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.*;
import com.googlecode.gwtptk.client.LeftRightPanel;

/**
 * Demo application with two demo cases - list without pages and list with paging controls.
 *
 * @author Richard "Virgo" Richter (virgo47@gmail.com)
 */
public class PtkDemo implements EntryPoint {
	private DemoServiceAsync service;
	private SimplePanel contentPanel;
	private LeftRightPanel upperBar = new LeftRightPanel(); // top bar

	@Override
	public void onModuleLoad() {
		service = (DemoServiceAsync) GWT.create(DemoService.class);
		ServiceDefTarget endpoint = (ServiceDefTarget) service;
		endpoint.setServiceEntryPoint(GWT.getModuleBaseURL() + "ptkdemo");

		contentPanel = new SimplePanel();
		contentPanel.addStyleName("content-screen");
		displayTopPanel();
		Panel uberPanel = new VerticalPanel();
		uberPanel.addStyleName("width-100-percent");
		uberPanel.add(upperBar);
		upperBar.setStyleName("upper-bar");
		uberPanel.add(contentPanel);
		RootPanel.get().add(uberPanel);

		contentPanel.setWidget(new SimpleScreen(this));
	}

	private void displayTopPanel() {
		HorizontalPanel leftPanel = new HorizontalPanel();
		Label titleLabel = new Label("GWT PTK Demo");
		titleLabel.addStyleName("top-title");
		leftPanel.add(titleLabel);

		Label separatorLabel = new Label("|");
		separatorLabel.addStyleName("top-text");
		leftPanel.add(separatorLabel);

		Label simpleLink = new Label("Simple");
		simpleLink.setStyleName("menulink");
		leftPanel.add(simpleLink);
		simpleLink.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent clickEvent) {
				contentPanel.setWidget(new SimpleScreen(PtkDemo.this));
			}
		});

		separatorLabel = new Label("|");
		separatorLabel.addStyleName("top-text");
		leftPanel.add(separatorLabel);

		Label pagingLink = new Label("Paging");
		pagingLink.setStyleName("menulink");
		leftPanel.add(pagingLink);
		pagingLink.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent clickEvent) {
				contentPanel.setWidget(new PagingScreen(PtkDemo.this));
			}
		});

		HorizontalPanel rightUpperBarWidget = new HorizontalPanel();
		Label usernameLabel = new Label("User: guest");
		usernameLabel.addStyleName("top-text");
		rightUpperBarWidget.add(usernameLabel);

		upperBar.setLeftWidget(leftPanel);
		upperBar.setRightWidget(rightUpperBarWidget);
	}

	public DemoServiceAsync getService() {
		return service;
	}
}